// src/stores/userStore.ts
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

export const useUserStore = defineStore('user', () => {
    const token = ref<string | null>(localStorage.getItem('token'));
    const router = useRouter();

    function setToken(newToken: string) {
        token.value = newToken;
        localStorage.setItem('token', newToken);
    }

    function clearToken() {
        token.value = null;
        localStorage.removeItem('token');
    }

    function logout() {
        clearToken();
        router.push('/login');
    }

    return {
        token,
        setToken,
        clearToken,
        logout
    };
});
